package com.theydream.qeechain.facebook;

import java.io.IOException;
import java.net.MalformedURLException;
import java.util.Date;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.theydream.qeechain.R;

public class AndroidFacebookSample extends Activity {

	private static final String FACEBOOK_APPID = "183524461720661";
	private static final String FACEBOOK_PERMISSION = "publish_stream";
	private static final String TAG = "FacebookSample";
	private static final String MSG = "Message from FacebookSample";

	private final Handler mFacebookHandler = new Handler();
	private TextView loginStatus;
	private FacebookConnector facebookConnector;

	final Runnable mUpdateFacebookNotification = new Runnable() {
		public void run() {
			Toast.makeText(getBaseContext(), "Facebook updated !",
					Toast.LENGTH_LONG).show();
		}
	};

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.facebook_main);
		this.facebookConnector = new FacebookConnector(FACEBOOK_APPID, this,
				getApplicationContext(), new String[] { FACEBOOK_PERMISSION });

		loginStatus = (TextView) findViewById(R.id.login_status);
		Button tweet = (Button) findViewById(R.id.btn_post);
		Button clearCredentials = (Button) findViewById(R.id.btn_clear_credentials);

		tweet.setOnClickListener(new View.OnClickListener() {
			/**
			 * Send a tweet. If the user hasn't authenticated to Tweeter yet,
			 * he'll be redirected via a browser to the twitter login page. Once
			 * the user authenticated, he'll authorize the Android application
			 * to send tweets on the users behalf.
			 */
			public void onClick(View v) {
				postMessage();
			}
		});

		clearCredentials.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				clearCredentials();
				updateLoginStatus();
			}
		});
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		this.facebookConnector.getFacebook().authorizeCallback(requestCode,
				resultCode, data);
	}

	@Override
	protected void onResume() {
		super.onResume();
		updateLoginStatus();
	}

	public void updateLoginStatus() {
		loginStatus.setText("Logged into Twitter : "
				+ facebookConnector.getFacebook().isSessionValid());
	}

	private String getFacebookMsg() {
		return MSG + " at " + new Date().toLocaleString();
	}

	public void postMessage() {

		if (facebookConnector.getFacebook().isSessionValid()) {
			postMessageInThread();
		} else {
			SessionEvents.AuthListener listener = new SessionEvents.AuthListener() {

				@Override
				public void onAuthSucceed() {
					postMessageInThread();
				}

				@Override
				public void onAuthFail(String error) {

				}
			};
			SessionEvents.addAuthListener(listener);
			facebookConnector.login();
		}
	}

	private void postMessageInThread() {
		Thread t = new Thread() {
			public void run() {

				try {
					facebookConnector.postMessageOnWall(getFacebookMsg());
					mFacebookHandler.post(mUpdateFacebookNotification);

					// byte[] data = null;
					//
					// Bitmap bi =
					// BitmapFactory.decodeFile("/sdcard/image.jpg");
					// ByteArrayOutputStream baos = new ByteArrayOutputStream();
					// bi.compress(Bitmap.CompressFormat.JPEG, 100, baos);
					// data = baos.toByteArray();
					//
					// Bundle params = new Bundle();
					// params.putString("method", "photos.upload");
					// params.putByteArray("picture", data);
					//
					// Facebook facebook = null;
					// AsyncFacebookRunner mAsyncRunner = new
					// AsyncFacebookRunner(facebook);
					// mAsyncRunner.request(null, params, "POST", new
					// RequestListener(), null);

				} catch (Exception ex) {
					Log.e(TAG, "Error sending msg", ex);
				}
			}
		};
		t.start();
	}

	private void clearCredentials() {
		try {
			facebookConnector.getFacebook().logout(getApplicationContext());
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}